package com.yanqu.road.server.channelserver.timer.washe;

import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.server.channelserver.manager.activity.washe.ChannelWasheActivity;
import com.yanqu.road.server.channelserver.manager.activity.washe.ChannelWasheMgr;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

public class ChannelWasheTask extends GeneralTask {

    private long checkConfigTime;
    private long handleMatchTime;
    private long handleExpireTime;
    private long handleRemoveExpireTime;

    // 巅峰赛生成名单时间
    private long jinBiaoPeakGenDataTime;
    // 巅峰赛创建房间
    private long jinBiaoPeakCreateRoomTime;
    // 巅峰赛开战
    private long jinBiaoPeakStartGameTime;
    // 阶段性奖励名单
    private long jinBiaoScoreStageTime;

    @Override
    public void doRun() {

        if (System.currentTimeMillis() > checkConfigTime) {
            checkConfigTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS * 5;
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                // 切换赛季
                try {
                    WasheConfig config = activity.getConfig();
                    if (config.isSeasonChange()) {
                        getLOGGER().debug("handle washe season change... activityId {}", config.getActivityInfo().getActivityId());
                        activity.reloadConfig(config.getActivityInfo());
                    }
                } catch (Exception e) {
                    getLOGGER().error("error : ", e);
                }
                // 每日盘面机制
                try {
                    activity.checkRefreshScene(false);
                } catch (Exception e) {
                    getLOGGER().error("error : ", e);
                }
            }
        }

        if (System.currentTimeMillis() - handleMatchTime > DateHelper.SECOND_MILLIONS * 3) {
            handleMatchTime = System.currentTimeMillis();
            // 匹配
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.matchByTimer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (System.currentTimeMillis() - handleExpireTime > DateHelper.SECOND_MILLIONS) {
            handleExpireTime = System.currentTimeMillis();
            // 超时
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.checkExpire();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (System.currentTimeMillis() - handleRemoveExpireTime > DateHelper.MINUTE_MILLIONS * 30) {
            handleRemoveExpireTime = System.currentTimeMillis();
            // 移除异常的活动
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.removeExpireErrorRoom();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (System.currentTimeMillis() - jinBiaoPeakGenDataTime > DateHelper.SECOND_MILLIONS * 5) {
            jinBiaoPeakGenDataTime = System.currentTimeMillis();
            // 锦标赛生成名单
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.jinBiaoPeakGenDataByTimer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (System.currentTimeMillis() - jinBiaoPeakCreateRoomTime > DateHelper.SECOND_MILLIONS * 5) {
            jinBiaoPeakCreateRoomTime = System.currentTimeMillis();
            // 锦标赛创建房间
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.jinBiaoPeakCreateRoomByTimer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        if (System.currentTimeMillis() - jinBiaoPeakStartGameTime > DateHelper.SECOND_MILLIONS) {
            jinBiaoPeakStartGameTime = System.currentTimeMillis();
            // 锦标赛开战
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.jinBiaoPeakStartGameByTimer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (System.currentTimeMillis() - jinBiaoScoreStageTime > DateHelper.SECOND_MILLIONS) {
            jinBiaoScoreStageTime = System.currentTimeMillis();
            for (ChannelWasheActivity activity : ChannelWasheMgr.getActivityMap().values()) {
                try {
                    activity.jinBiaoScoreStageByTimer();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
